package com.agou.app.admin.business.user.action;

import java.io.IOException;
import java.sql.SQLException;

import com.agou.app.admin.dbconfig.bean.TSAdminUser;
import com.agou.core.ss.SSException;
import com.agou.core.ss.SSTrace;
import com.agou.ssh.core.base.ActionResultSet;
import com.agou.ssh.core.base.BOResultSet;
import com.agou.ssh.web.WebUtil;
import com.opensymphony.xwork2.Action;

public class UserAction extends IUserAction {
	private static final long serialVersionUID = -8357180487207298194L;
	private static SSTrace ssTrace = SSTrace.getInstance(UserAction.class);
	/**
	 * 用户登陆准备
	 * 
	 * @return
	 * @throws SSException
	 */
	public String initUserLogin() throws SSException, SQLException, IOException {
		try {
			ssTrace.debug("initUserLogin", "准备用户登陆页面(initUserLogin)");
			return Action.LOGIN;
		}finally{
			//this.destroy();//
		}
	}

	/** 用户登陆 **/
	public String userLogin() throws SSException, SQLException, IOException {
		ActionResultSet actionResult = new ActionResultSet(UserAction.class);
		try {
			ssTrace.debug("userLogin", "用户[#0]开始登陆.....",userVo);
			this.jdbcBeging(true, true);// 设置使用事务，并且自动管理
			this.getUserBo().setJdbcSession(this.getJdbcSession());// bo对象如果使用数据库操作，需要进行初始化
			BOResultSet boResultSet = this.getUserBo().userLogin(userVo);
			actionResult.addBoResultSet("user_login_bo", boResultSet);
			if (boResultSet.IsSuccess == false) {
				actionResult.IsSuccess = false;
				actionResult.ReturnCode = boResultSet.ReturnCode;
				actionResult.ReturnInfo = boResultSet.ReturnInfo;
				return Action.LOGIN;//登陆失败进入用户登陆页面
			}
			// 设置应用环境
			TSAdminUser tAdminUser = (TSAdminUser) boResultSet.getDataMapValue("tsAdminUser");
			WebUtil.createAppContext("ssh",tAdminUser.getUserId(), tAdminUser.getUserLoginId(),tAdminUser.getUserName());
			return "login_success";// 登陆成功
		} finally {
			this.setActionResultSet(actionResult);
			this.destroy();//
		}
	}
}
